/*****************************************************************************
 
 File:   OTKWorkspaceManager.java
 
 Date          Build     Author        Version    Comments
29-May-12    P-20-54    rkothari          $$1        Created

****************************************************************************/
package com.ptc.otkjavaexamples.ui;

import com.ptc.cipjava.jxthrowable;
import com.ptc.cipjava.stringseq;
import com.ptc.uifc.uifcCore.RowPositionData;
import com.ptc.uifc.uifcCore.SelectionPolicy;
import com.ptc.uifc.uifcCore.uifcCore;
import com.ptc.uifc.uifcLabel.Label;
import com.ptc.uifc.uifcLabel.uifcLabel;
import com.ptc.uifc.uifcTable.DefaultTableListener;
import com.ptc.uifc.uifcTable.Table;
import com.ptc.uifc.uifcTable.TableCell;
import com.ptc.uifc.uifcTable.TableRow;
import com.ptc.uifc.uifcTable.uifcTable;

/*
 * Populate server list in a Table with icon , name and status of the server * 
 */
public class OTKServerManager extends DefaultTableListener
{
    private Table serverTable;
    private String serverNames [] = {"<No Server>","otk.ptc.com","toolkit.ptc.com", "jlink.ptc.com","weblink.ptc.com","vbtoolkit.ptc.com"};
    private String serverStatus [] = {" ","online", "offline", "offline","offline","offline"};
    private String serverIcons [] = {"otk_arrow","otk_server16x16", "otk_server16x16", "otk_server16x16","otk_server16x16","otk_server16x16"};	
    
    /*
     * Set Selection policies for row and cell selections of the table	 
     */
    public OTKServerManager()
    {		
	try
	    {
		serverTable = uifcTable.TableFind(OTKServerRegistryDialog.OTK_SERVER_DIALOG,OTKServerRegistryDialog.OTK_SERVER_TABLE);
		serverTable.AddActionListener(new OTKWorkspaceManager());
		serverTable.SetHeight(serverNames.length + 1);
		serverTable.SetRowHighlight(true);
		serverTable.SetRowSelectionPolicy(SelectionPolicy.SELECTION_POLICY_EXTENDED);
		serverTable.SetCellSelectionPolicy(SelectionPolicy.SELECTION_POLICY_EXTENDED);
		serverTable.SetSelectionVisible(true);
	    } 
	catch (jxthrowable e)
	    {
		e.printStackTrace();
	    }
	
    }	
    /*
     *  Populate server details in the table	 
     */
    public void populateServers()
    {
	
	for(int ii=0;ii<serverNames.length;ii++)
	    {			
		try
		    {
			/*
			 *  Create a row by the name of the Server and insert it in the table
			 */
			TableRow serverRow = uifcTable.TableRowDefine(serverNames[ii]);				
			RowPositionData rowPos = uifcCore.RowPositionData_Create();			
			rowPos.SetIndex(ii);			
			serverTable.InsertRow(serverRow, rowPos);	
			
			/*
			 *  Locate server cell and fill it with server name
			 */			
			TableCell serverCell =  uifcTable.TableCellFind(serverTable.GetDialog(), serverTable.GetComponent(), serverNames[ii], "server");
			serverCell.SetText(serverNames[ii]);
			
			/*
			 *  Define a label and set an icon image to it
			 * Insert that label to the table
			 * Locate the icon cell and set label as its component name
			 */			
			Label iconLabel = uifcLabel.LabelDefine(serverRow + "_label");
			iconLabel.SetImage(serverIcons[ii]);				
			serverTable.InsertChild(iconLabel);				
			
			TableCell iconCell =  uifcTable.TableCellFind(serverTable.GetDialog(), serverTable.GetComponent(), serverNames[ii], "image");
			iconCell.SetComponentName(serverRow + "_label");				
			
			/*
			 *	Locate server status cell and fill it with server status
			 */
			TableCell statusCell =  uifcTable.TableCellFind(serverTable.GetDialog(), serverTable.GetComponent(), serverNames[ii], "status");
			statusCell.SetText(serverStatus[ii]);
			
		    } 
		catch (jxthrowable e)
		    {			
			e.printStackTrace();
		    }			
	    }
	
	/*		   
	 * Set first server row as selected 
	 */
	try
	    {
		stringseq selectedRow = stringseq.create();
		selectedRow.append(serverNames[0]);
		selectedRow.append("server");		
		
		serverTable.SetSelectedCellNameArray(selectedRow);			
	    } 
	catch (jxthrowable e)
	    {			
		e.printStackTrace();
	    }
	
	
    }	
    
}
